<template>
	<div class="page-container">
		<el-dialog :title="myParam.title" width="90%" :visible.sync="selectDetVisible"
			:close-on-click-modal="false" :before-close="handleClose" @opened="findPage(1)">
			<div class="gk-layout-container"> 
				<div class="gk-layout-main" style="height:500px">
					<my-table ref="zttable" :data="listResult"   @selection-change="selectionChange" :loading="loading">
						<el-table-column :label="$t('action.desc')" width="50" fixed="left" header-align="center"
							align="center">
							<template slot-scope="scope"> {{ scope.$index + 1 }} </template>
						</el-table-column>
						<el-table-column type="selection" width="40" :selectable="selectable"></el-table-column>
						<column prop="itemId" label="物料名称" header-align="left" align="left" width="450" :formatter="itemFilter" />
						<column prop="countDb" label="数量" :formatter="countFormat" />
						<column prop="status" label="状态" :formatter="selectionFormat1" />
						<column prop="boxCode" label="托盘码" />
					<el-table-column header-align="center" align="center" width="250" :label="$t('action.operation')">
					<template slot-scope="scope"> 
						<kt-button label="拆分" v-if="scope.row.status == 9" perms="core:stockDetail:Out"
							@click="splitDet(scope.$index, scope.row)" /> 
					</template>
				</el-table-column>
					</my-table>
				</div> 
			</div>
		</el-dialog>
	</div>
</template>

<script>

import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
import { formatDay } from "@/utils/datetime"
import Column from "@/views/Core/Column"
import MyTable from "@/views/Core/MyTable"
import SQuery from "@/components/Kj/SQuery"
import CoreStockView from "@/dialog/CoreStockView"
import ItemSelect from "@/views/Basic/Material/ItemSelect"
import { dictsFormat, getItemName } from "@/utils/myUtil"
import XLSX from "xlsx";
export default {
	name: 'CoreStock',
	components: {
		KtButton,
		Column,
		 SQuery,
		MyTable,
		ItemSelect,
		CoreStockView
	},
	props: {
		myParam: Object,
		selectDetVisible: {
			type: Boolean
		}

	},
	data() {
		return {
			size: 'small', 
			loading: false, 
			componentKey: 0,
			listResult: [] 
		}
	},
	methods: {
		// 获取分页数据
		findPage: function (data) {
			this.loading=true;
			this.$api.doo.selectCsd(this.myParam).then((res) => {
				this.listResult = res.data 
				this.$nextTick(() => {
					var contetArray = this.listResult;
					for (var i = 0; i < contetArray.length; i++) {
						var d = contetArray[i];
						if (d.dooDetId == this.myParam.id) {
							this.$refs.zttable.$children[0].toggleRowSelection(d, true);
						}
					}
					this.loading = false;
				})
			})
		},
	 
	 

		 
		selectable: function (row) {
			return true;
		},
		selectionChange: function (selections) {

			
			this.selections = selections
			if (this.loading) {
				return;
			}
			var param = this.myParam.id;
			var del = "/0";
			var add = "/0";
			for (var i in this.listResult) {
				var d = this.listResult[i];
				if (d.dooDetId == this.myParam.id) {
					var has = false;
					for (var j in selections) {
						var s = selections[j]
						if (s.id == d.id) {
							has = true;
						}
					}
					if (!has) {
						del += "," + d.id;
					}
				}
			}
			for (var s in selections) {
				add += "," + selections[s].id;

			}
			this.$api.coreStockDet.changeCsdDooDetId(param + add + del).then((res) => {
				this.findPage(-1)
			})
		},
 
	countFormat: function (row, column, cellValue, index) {
			return cellValue + dictsFormat("units", row.unit)
		},
		selectionFormat1: function (row, column, cellValue, index) {
			return dictsFormat("storeStatus", cellValue)
		},

		// 时间格式化
		dateFormat: function (row, column, cellValue, index) {
			return format(row[column.property])
		},
		// 时间格式化
		dateFormatDay: function (row, column, cellValue, index) {
			return formatDay(row[column.property])
		},
		itemFilter: function (row, column, cellValue, index) {
			return getItemName(cellValue)
		},
		// 生成任务
		splitDet: function (index, row) {
		 
			this.$prompt(
                '请数量：', 
                '提示',  {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type:"warning",
                    inputValue: '0',
					inputPattern:/^([0-9]+([.][0-9]*)?)$/,
                    inputErrorMessage: "请输入数字",
                     
                }).then(({value}) => {
					row.countDb=value;
					debugger
                 	this.$api.coreStock.splitDetEngine(row).then((res) => {
						if (res.code == 200) {
							this.$message({ message: '操作成功', type: 'success' })
							this.findPage(-1)   //这里有bug
						} else {
							this.$message({ message: res.msg, type: 'error' })
						}
					})
                }).catch((err) => {
                    console.log(err);
                }); 
 
		},
		selectionFormat: function (row, column, cellValue, index) {
			let key = ""
			let propt = column.property;
			if (propt == "status") {
				key = "stockStatus"
			}
			else if (propt == "storeStatus") {
				key = "storeStatus"
			} else if (propt == "locked") {
				key = "isLocked"
			} else if (propt == "wh") {
				key = "wh"
			}

			return dictsFormat(key, cellValue)
		},
		handleClose: function () { 
			this.$emit('selectDetClose', {})
		 
		},

	},
	mounted() {
	 
	}
}
</script>

<style scoped></style>